package com.lagou.edu.controller;

import com.lagou.edu.service.UserService;
import com.lagou.edu.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/register/{email}/{password}/{code}")
    public Result register(@PathVariable String email,
                           @PathVariable String password,
                           @PathVariable String code,
                           HttpServletResponse response) {
        return userService.register(email,password,code,response);
    }

    @GetMapping("/isRegistered/{email}")
    public Boolean isRegistered(@PathVariable String email) {
        return userService.isRegistered(email);
    }

    @GetMapping("/login/{email}/{password}")
    public Result login(@PathVariable String email,
                        @PathVariable String password,
                        HttpServletRequest request,
                        HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getValue());
            }
        }
        return userService.login(email,password,response);
    }

    @GetMapping("/info/{token}")
    public Result userInfo(@PathVariable String token,
                           HttpServletRequest request,
                           HttpServletResponse response) {
        return userService.userInfo(token);
    }

    @GetMapping("/logout")
    public void userInfo(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        System.out.println(">>>>>>>>>>>>" + cookies);
        if(cookies != null){
            for (Cookie cookie : cookies) {
                cookie.setMaxAge(0);
            }
        }
    }




}
